/**
 * Author: Dmitry Ustimov
 * Date: 5/10/13
 * Time: 4:17 PM
 */

package com.gmail.dmitryustimov.ms.java7.visitor.mod_actionmap.example.example2;

import java.util.HashMap;
import java.util.Map;

public class Visitor<T> {

    private Map<Class<? extends T>, VisitAction<? extends T>> actions = new HashMap<>();

    public void addVisitAction(Class<? extends T> clazz, VisitAction<? extends T> visitAction) {
        actions.put(clazz, visitAction);
    }

    public <E extends T> VisitAction<E> getAction(Class<E> clazz) {
        return (VisitAction<E>) actions.get(clazz);
    }

    public <E extends T> void visit(E element) {
        VisitAction<E> action = getAction(((Class<E>)element.getClass()));
        if (action != null) {
            action.visit(element);
        }
    }
}
